<?php


class EditingMethodsComponent extends Object {

/******************************************************************************/

var $components = array(
	'RequestHandler',
	'Admin.AutomateMethods',
	'Admin.SimpleMethods'
);

/******************************************************************************/

var $Controller = NULL;

var $settings = array();

/******************************************************************************/

function initialize(&$Controller, $settings = array()) {
	$this->Controller = &$Controller;
	$this->settings = (array)$settings;
}

/******************************************************************************/

function add($parent_or_rubric_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		if (!isset($ctrl->data[$ctrl->Model->name]) || !is_array($ctrl->data[$ctrl->Model->name])) {
			$ctrl->data[$ctrl->Model->name] = array();
		}
		// merge
		if ($ctrl->Model->isAttachment() && isset($ctrl->params['named']['model'], $ctrl->params['named']['foreign_key'])) {
			$ctrl->data[$ctrl->Model->name]['model'] = $ctrl->params['named']['model'];
			$ctrl->data[$ctrl->Model->name]['foreign_key'] = $ctrl->params['named']['foreign_key'];
		}
		if (isset($ctrl->params['named']['_merge']) && (bool)$ctrl->params['named']['_merge']) {
			$params_named = array_intersect_key($ctrl->params['named'], $ctrl->Model->schema());
			$ctrl->data[$ctrl->Model->name] = Set::merge($params_named, $ctrl->data[$ctrl->Model->name]);
		}
		// process
		if ($this->RequestHandler->isPost()) {
			$ctrl->Model->id = FALSE;
			if ((bool)$ctrl->Model->create($ctrl->data, TRUE) && (bool)$ctrl->Model->save()) {
				if (isset($ctrl->params['form']['_close']) && (bool)$ctrl->params['form']['_close']) {
					$this->SimpleMethods->back($ctrl->Model->id, CODE_ADDED);
				} elseif (isset($ctrl->params['form']['_close']) && (bool)$ctrl->params['form']['_close']) {
					$this->SimpleMethods->back(FALSE, CODE_ADDED);
				} else {
					$ctrl->redirect(array('action' => 'edit', 'id' => $ctrl->Model->id, 'autoPage' => TRUE), CODE_ADDED);
				}
			} else {
				$ctrl->redirect(array('action' => 'add', $parent_or_rubric_id, 'autoPage' => TRUE), CODE_ERROR);
			}
		} else {
			$ctrl->set(array('creating' => TRUE, 'created' => FALSE));
			$ctrl->set('parent_or_rubric_id', $parent_or_rubric_id);
			if ($ctrl->Model->isTree()) {
				$ctrl->set('parent_id', $parent_id = $parent_or_rubric_id);
				$ctrl->data[$ctrl->Model->name][$ctrl->Model->getParentKey()] = $parent_id;
			} elseif ($ctrl->Model->hasRubric()) {
				$ctrl->set('rubric_id', $rubric_id = $parent_or_rubric_id);
				$ctrl->data[$ctrl->Model->name][$ctrl->Model->getRubricKey()] = $rubric_id;
			}
			$ctrl->set('tab_schema', $tab_schema = $ctrl->Model->getTabSchema(TRUE));
			// render
			if ($ctrl->autoRender && $ctrl->automateRender) {
				$ctrl->render();
			}
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

function edit($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		if (!isset($ctrl->data[$ctrl->Model->name]) || !is_array($ctrl->data[$ctrl->Model->name])) {
			$ctrl->data[$ctrl->Model->name] = array();
		}
		// id
		$ctrl->data[$ctrl->Model->name][$ctrl->Model->primaryKey] = $id;
		// merge
		if ($ctrl->Model->isAttachment() && isset($ctrl->params['named']['model'], $ctrl->params['named']['foreign_key'])) {
			$ctrl->data[$ctrl->Model->name]['model'] = $ctrl->params['named']['model'];
			$ctrl->data[$ctrl->Model->name]['foreign_key'] = $ctrl->params['named']['foreign_key'];
		}
		if (isset($ctrl->params['named']['_merge']) && (bool)$ctrl->params['named']['_merge']) {
			$params_named = array_intersect_key($ctrl->params['named'], $ctrl->Model->schema());
			$ctrl->data[$ctrl->Model->name] = Set::merge($params_named, $ctrl->data[$ctrl->Model->name]);
		}
		// process
		if ($this->RequestHandler->isPost() || $this->RequestHandler->isPut()) {
			$ctrl->Model->id = $id;
			if ((bool)$ctrl->Model->save($ctrl->data)) {
				if (isset($ctrl->params['form']['_close']) && ($ctrl->params['form']['_close'] == '1')) {
					$this->SimpleMethods->back($id, CODE_EDITED);
				} elseif (isset($ctrl->params['form']['_close']) && ($ctrl->params['form']['_close'] == '2')) {
					$this->SimpleMethods->back(FALSE, CODE_EDITED);
				} else {
					$ctrl->redirect(array('action' => 'edit', 'id' => $id, 'autoPage' => TRUE), CODE_EDITED);
				}
			} else {
				$ctrl->redirect(array('action' => 'edit', 'id' => $id, 'autoPage' => TRUE), CODE_ERROR);
			}
		} else {
			$ctrl->set(array('creating' => FALSE, 'created' => TRUE));
			$ctrl->set('id', $id);
			$ctrl->set('row', $row = $ctrl->data = $ctrl->Model->find('byid', array('value' => $id, 'contain' => array_keys($ctrl->Model->hasAndBelongsToMany))));
			$ctrl->set('tab_schema', $tab_schema = $ctrl->Model->getTabSchema(TRUE, $id));
			// caption
			if (isset($ctrl->data[$ctrl->Model->name][$ctrl->Model->displayField])) {
				$ctrl->PageTitle->dataCaption = $ctrl->data[$ctrl->Model->name][$ctrl->Model->displayField];
			}
			// render
			if ($ctrl->autoRender && $ctrl->automateRender) {
				$ctrl->render();
			}
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

function delete($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$index_url = $ctrl->Model->getIndexPageURL($id);
		$ctrl->Model->id = $id;
		if ($ctrl->Model->delete($id)) {
			$ctrl->redirect($index_url, CODE_DELETED);
		} else {
			$ctrl->redirect(array('action' => 'edit', 'id' => $id, 'autoPage' => TRUE), CODE_ERROR);
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

}


?>